package 第七周.experiment;

import static java.lang.StrictMath.round;
import static java.lang.StrictMath.sqrt;

public class Rename {
    private final Rename1 rename1 = new Rename1(this);
    // 定义属性并生成getter,setter
    private double R;
    private double I;

    // 定义构造函数.
    public Rename(double R, double I) {
        this.setR(R);
        this.setI(I);
    }

    public double getR() {
        return rename1.getR();
    }
    public double getI() {
        return rename1.getI();
    }

    public boolean equals(Rename a){
        return rename1.equals(a);
    }
    //定义公有方法:加减乘除.
    public Rename ComplexAdd(Rename a) {
        return rename1.ComplexAdd(a);
    }

    public Rename ComplexSub(Rename a) {
        return rename1.ComplexSub(a);
    }

    public Rename ComplexMulti(Rename a) {
        double r = rename1.getR() * a.getR() - rename1.getI() * a.getI();
        double i = rename1.getR() * a.getI() - rename1.getI() * a.getR();
        return new Rename(rename1.getR() * a.getR() - rename1.getI() * a.getI(), rename1.getR() * a.getI() + rename1.getI() * a.getR());
    }

    public Rename ComplexDiv(Rename a) {
        double d =sqrt(a.getR() * a.getR()) + sqrt(a.getI() * a.getI());
        double e = (rename1.getR() * a.getI() - rename1.getI() * a.getR());
        return new Rename((rename1.getR() * a.getR() + rename1.getI() * a.getI()) / d, round(e / d));
    }

    public String toString() {
        String result = "";
        if (rename1.getI() == 0.0)
            result = rename1.getR() +"";
        else if (rename1.getR() == 0.0)
            result = rename1.getI() + "i";
        else
            if(rename1.getI() >0.0)
              result = rename1.getR() +""+ "+" + rename1.getI() + "i";
            else
                result= rename1.getR() +""+ rename1.getI() +"i";
        return result;
    }

    public void setR(double r) {
        R = r;
    }

    public void setI(double i) {
        I = i;
    }
}
